#!/usr/bin/env python
# just a dummy class to pull metadata from Bruker Frame (.sfrm) files

from string import strip

class SFRM:
  def __init__(self,file):
    self.tbufsize = 80
    self.file = file
    
  def getMetadata(self):
    buf = self.file.read(self.tbufsize)
    if not buf.startswith("FORMAT :"):
      raise Exception("File is not .sfrm format")
    meta = {}
    version = 0
    while True:
       if buf[0:8] =='VERSION:': 
         version = buf[8:].strip()
       #if buf[0:4] =='\x1a\x04\x00.': # end of metadata 
       #  break
       if buf[0:2] =='\x1a\x04': # end of metadata 
         break
       if buf[-1] =='\x04': # end of metadata for version 15 
         break
       parts = buf.split(':')
       if len(parts)<2:
	 print parts
         raise Exception(".sfrm file parsing error")
       key=parts[0].strip()
       value = parts[1].strip()
       if meta.has_key(key) and meta[key] != '':
	 meta[key] = meta[key] + '\n' + value
       else:
         meta[key]= value
       buf = self.file.read(self.tbufsize)
    self.meta = meta
    return meta
	
if __name__ == '__main__':
   import sys
   fileList = sys.argv[1:]
   print fileList
   for arg in fileList:
     print arg
     file= open(arg)
     sf = SFRM(file)
     meta =  sf.getMetadata()
     file.close()
     for key in meta.keys():
       print "%7s : %s" % (key,meta[key])

